package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.util.Random;

public class Ex1 {
    public static void main(String[] args) {
        File file = new File("g1.jpg");
        try {
            var src = ImageIO.read(file);
            int w = src.getWidth();
            int h = src.getHeight();
            var g = src.createGraphics();

            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);


            g.setColor(Color.ORANGE);
            g.drawLine(0, h / 2, w, h / 2);

            g.setColor(Color.WHITE);
            g.drawLine(w / 2, 0, w / 2, h);


            //颜色
            var c = new Color(0, 0, 0, 120);

            g.setColor(c);
            //字体
            Font font = Font.createFont(Font.TRUETYPE_FONT, new File("fots/b.ttf"));
            font = font.deriveFont(Font.BOLD, 55);
            g.setFont(font);

            //drawString()将字符串画到画布
            int x = 10;
            int y = 55;
            String text = "版权所有-13014577033";

            FontMetrics fm = g.getFontMetrics(font);
            int stringWidth = fm.stringWidth(text);
            int stringAscent = fm.getAscent();

            //左上角
            x = 10;
            y = stringAscent + 10;

            //右下角
            x = w - stringWidth - 10;
            y = h - 10;

            //正中央
            x = (w - stringWidth) / 2;
            y = (h + stringAscent) / 2;

            //下边水平中央
            x = (w - stringWidth) / 2;
            y = h - 10;

            Random rand = new Random();
            x = rand.nextInt(10,w - 10 - stringWidth);
            y = rand.nextInt(10+stringAscent,h - 10);

            g.drawString(text, x, y);
            //g.setColor(new Color(255,255,255,100));
            //g.drawString(text,x-1,y-1);
            //g.setColor(new Color(255,255,255,255));
            //g.drawString(text,x,y);
            //g.setColor(new Color(0,0,0,100));
            //g.drawString(text,x+1,y+1);

            //g.setColor(new Color(255,255,255,255));
            //g.draw3DRect(100,100,300,300,true);

            g.dispose();

            file = new File("g2.jpg");
            ImageIO.write(src, "jpg", file);
            Runtime.getRuntime().exec("cmd /k start " + file.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
